﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Dto.Customer;
using CRM.Admin.Contracts.Model.Customer;
using CRM.Admin.Services.Service.Customer;
using KingsSharp.Common.Filter;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Core.Data.Extensions;
using KingsSharp.Web.UI;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;

namespace CRM.Admin.Areas.Customer.Controllers
{
    public class CustomerAgreementController : Controller
    {
        public ICustomerAgreementService CustomerAgreementService { get; set; }
        // GET: Customer/CustomerAgreement
        public ActionResult Insert(Guid CustomerId)
        {
            ViewBag.CustomerId = CustomerId;
            return View();
        }
        public ActionResult Edit(Guid Id)
        {
            var entity = CustomerAgreementService.CustomerAgreements.FirstOrDefault(a=>a.Id==Id).MapTo<CustomerAgreementDto>();

            return View(entity);
        }
        /// <summary>
        /// 添加顾客协议
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ActionResult Add(CustomerAgreementDto dto)
        {
            dto.CheckNotNull(nameof(dto));

            var result = CustomerAgreementService.Insert(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "顾客协议添加成功";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        /// <summary>
        /// 更新顾客协议
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ActionResult Update(CustomerAgreementDto dto)
        {
            dto.CheckNotNull(nameof(dto));
            var result = CustomerAgreementService.Update(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "顾客协议更新成功";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        /// <summary>
        /// 删除顾客
        /// </summary>
        /// <returns></returns协议>
        public ActionResult Delete(Guid dto)
        {            
            var result = CustomerAgreementService.Delete(dto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "顾客协议删除成功";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        /// <summary>
        /// 查询顾客协议
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCustomerAgreementData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<CustomerAgreement, bool>> predicate = FilterHelper.GetExpression<CustomerAgreement>(request.FilterGroup);
            int total;
            var data = CustomerAgreementService.CustomerAgreements.
                Where<CustomerAgreement, Guid>(predicate, request.PageCondition, out total).
                Where(a => a.IsDeleted == false).Select(s => new
                {
                    s.Id,
                    s.ProdName,
                    s.ProtocolQuantity,
                    s.Year,
                    s.moth,
                    s.CompletionQuantity
                }).ToList();
            return Json(new GridData<object>(data, total), JsonRequestBehavior.AllowGet);
        }
    }
}